package Ep06_BinaryTree.BT24_0530;

import Ep06_BinaryTree.TreeNode;

import java.util.ArrayList;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/6/24 16:27
 * 说明：leecode 0530
 * 思路：
 *      暴力解法：中序遍历得到顺序数组，然后通过循环计算最小差值
 */

public class Mine {
    private static ArrayList<Integer> arr = new ArrayList<>();
    private static int min = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        traverse(root, arr);
        // 得到严格升序的数组
        int[] array = new int[arr.size()];
        for (int i = 0; i < arr.size(); i++) {
            array[i] = arr.get(i);
        }
        for (int i = 0; i < array.length - 1; i++) {
            int difference = Math.abs(array[i + 1] - array[i]);
            min = Math.min(difference, min);
            System.out.println(min);
        }
        return min;
    }

    private void traverse(TreeNode root, ArrayList<Integer> arr){
        if (root == null) return;
        traverse(root.left, arr);
        arr.add(root.val);
        traverse(root.right, arr);
    }
}
